package com.ionehe.group.common.zzd.service;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Throwables;
import com.ionehe.group.common.io.Response;
import com.ionehe.group.common.zzd.dto.ZzdContent;
import com.ionehe.group.common.zzd.dto.ZzdResponse;
import com.ionehe.group.common.zzd.resp.ZzdUserAccount;
import com.ionehe.group.common.zzd.ZzdUserService;
import com.ionehe.group.common.zzd.session.ZzdSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Copyright (c) 2021 ionehe.com
 * Date: 2021/8/4
 * Time: 11:17 下午
 *
 * @author 2021年 <a href="mailto:a.ionehe.com">秀</a>
 */
@Service
@Slf4j
public class ZzdUserServiceImpl implements ZzdUserService {
    @Resource
    private ZzdSession zzdSession;

    @Override
    public Response<ZzdUserAccount> getDingtalkAppUser(String authCode) {
        try {
            ZzdResponse<ZzdContent<ZzdUserAccount>> zzdResponse = this.zzdSession.getDingtalkAppUser(authCode);

            if (zzdResponse != null && zzdResponse.getContent() != null && ((ZzdContent) zzdResponse.getContent()).getData() != null) {
                ZzdUserAccount userAccount = (ZzdUserAccount) ((ZzdContent) zzdResponse.getContent()).getData();
                return Response.yes(userAccount);
            }

            log.error("Get zzd user info failed, response:{}", JSON.toJSONString(zzdResponse));

            return Response.no("获取用户信息失败");
        } catch (Exception e) {
            log.error("Get zzd user info error! cause:{}", Throwables.getStackTraceAsString(e));
            return Response.no("获取用户信息异常");
        }
    }
}
